package com.example.pagesliderdemo.slice;

import com.example.pagesliderdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        ArrayList<ItemModel> dataItems = new ArrayList<ItemModel>();
        dataItems.add(new ItemModel("页面1"));
        dataItems.add(new ItemModel("页面2"));
        dataItems.add(new ItemModel("页面3"));
        dataItems.add(new ItemModel("页面4"));

        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageSlider.setOrientation(Component.VERTICAL);
        pageSlider.setProvider(new PageSliderDataProvider(dataItems, getContext()));

        // 添加事件监听
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {
                // 页面滑动时回调，3个参数分别从不同维度描述了页面的滑动位置
                // itemPos: 当前页面的索引下标
                // itemPosOffset: 当前页面的偏移量比例，0-1之前
                // itemPosOffsetPixels：当前页面的偏移量像素数
                HiLog.debug(new HiLogLabel(0,0,"PageSlider"), "onPageSliding");
            }

            @Override
            public void onPageSlideStateChanged(int state) {
                // 页面滑动状态改变时回调
                // state：0 默认状态 1 用户正在拖拽状态 2 滑动中状态
                HiLog.debug(new HiLogLabel(0,0,"PageSlider"), "onPageSlideStateChanged");
            }

            @Override
            public void onPageChosen(int itemPos) {
                // 当一个新的页面被选中时回调
                // itemPos：选中的页面的索引下标
                HiLog.debug(new HiLogLabel(0,0,"PageSlider"), "onPageChosen");
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
